﻿using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WorkFlow.Data;
using WorkFlow.Data.Model;

namespace WorkFlow.Pages
{
    public partial class Detail
    {
        [Parameter]
        public string ID { get; set; }
        private string _workFlowName;
        private string _User;
        private int _Status = -1;
        private string _Message;
        private DateTime _StartTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:01"));
        private DateTime _EndTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
        protected override void OnParametersSet()
        {
            commentTool.ShowNav = false;
            base.OnParametersSet();
        }
        protected override void OnAfterRender(bool firstRender)
        {
            base.OnAfterRender(firstRender);
            if (firstRender)
            {
                Search();
            }
        }
        //protected override Task OnParametersSetAsync()
        //{
        //    var res = ProtectedSessionStore.GetAsync<string>(id);
        //    //id = res.Value;

        //    workFlowDetailList = _DataManager.GetWorkFlowDetail(id);
        //    UpdateUI();
        //    base.OnParametersSetAsync();
        //}

        private void SearchParaters(string detailID)
        {
            parameterEntityList = new List<ParameterEntity>();
            List<ParameterEntity> inParameterEntityList = _WorkFlowTool.GetInParametersByDetailID(detailID);
            List<ParameterEntity> outParameterEntityList = _WorkFlowTool.GetOutParametersByDetailID(detailID);
            parameterEntityList.AddRange(inParameterEntityList);
            foreach (var item in outParameterEntityList)
            {
                if (parameterEntityList.FindAll(x => x.Name == item.Name).Count == 0)
                {
                    parameterEntityList.Add(item);
                }
            }
            UpdateUI();
        }
        private void Search()
        {
            workFlowInfo = _DataManager.GetWorkFlowInfoByID(ID);
            workFlowDetailList = _DataManager.GetWorkFlowDetailByMainID(ID);
            UpdateUI();
        }

        private async void UpdateUI()
        {
            await InvokeAsync(() => StateHasChanged());
        }
    }
}
